package edu.mobbuzz.page.menu.outbox;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;

import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Border;
import edu.mobbuzz.bean.Message;
import edu.mobbuzz.facade.MessageFacade;
import edu.mobbuzz.page.menu.Menu;
import edu.mobbuzz.page.menu.OutboxMenu;
import edu.mobbuzz.page.menu.inbox.MessagePage;
import edu.mobbuzz.util.string.MenuString;
import edu.mobbuzz.renderer.MessageRenderer;
import edu.mobbuzz.util.string.ModeString;

public class OutboxPage extends Menu{
    private Form outboxForm;
    private Container outboxPanel;
    private Label menuTitle;
    private DefaultListModel messageListModel;
    private List messagelist;

    private Command backCmd;
    private Command deleteCmd;
    private Command deleteAllCmd;
    private Command shareCmd;
    private Command detailCmd;
    private Command newCmd;
    private MessageFacade messageFacade;

    public OutboxPage(){
        outboxForm = new Form();
        outboxPanel = new Container();
        menuTitle = new Label(getMenuTitle());

        messageListModel = new DefaultListModel(getMessageFacade().displayOutboxMessageList());

        detailCmd = new Command("Open") {
            public void actionPerformed(ActionEvent evt) {
                openMessage(ModeString.EDIT);
            }
        };

        shareCmd = new Command("Share") {
            public void actionPerformed(ActionEvent evt) {
               shareMessage(ModeString.EDIT);
            }
        };

        deleteCmd = new Command("Delete") {
            public void actionPerformed(ActionEvent evt) {
               deleteMessage();
            }
        };

        deleteAllCmd = new Command("Delete All"){
            public void actionPerformed(ActionEvent evt) {
               deleteAllMessage();
            }
        };

        backCmd = new Command("Back") {
            public void actionPerformed(ActionEvent evt) {
                backToOutboxMenu();
            }
        };

        
    }

    public void showPage(){
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);

        outboxPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        outboxPanel.addComponent(menuTitle);
        outboxPanel.addComponent(createList(messageListModel, List.VERTICAL, new MessageRenderer()));

        outboxForm.setLayout(new BorderLayout());
        outboxForm.addComponent(BorderLayout.CENTER, outboxPanel);
        outboxForm.addCommand(backCmd);
        if(messageListModel.getSize()!=0){            
            outboxForm.addCommand(deleteAllCmd);
            outboxForm.addCommand(deleteCmd);
            outboxForm.addCommand(shareCmd);
        }
        outboxForm.addCommand(newCmd);
        if(messageListModel.getSize()!=0){
            outboxForm.addCommand(detailCmd);
        }
        outboxForm.setCommandListener(getActionlistener());

        outboxForm.setScrollableY(true);
        outboxForm.setSmoothScrolling(true);
        outboxForm.show();

    }



    private List createList(DefaultListModel messageListModel, int orientation, ListCellRenderer renderer) {
        messagelist = new List(messageListModel){
            public void keyPressed(int keyCode){
               gameActionPerformed(keyCode);
            }
        };
        messagelist.getStyle().setBgTransparency(0);
        messagelist.getStyle().setBorder(Border.getEmpty());
        messagelist.setListCellRenderer(renderer);
        messagelist.setOrientation(orientation);

        return messagelist;
    }

    public void gameActionPerformed(int keyCode){
        int game = Display.getInstance().getGameAction(keyCode);

        if(game == Display.GAME_FIRE){
            openMessage(ModeString.EDIT);
        }else if(game == Display.GAME_DOWN){

            if(messagelist.getSelectedIndex()<(messagelist.size()-1)){
               messagelist.setSelectedIndex(messagelist.getSelectedIndex()+1);
            }else{
                messagelist.setSelectedIndex(0);
            }

        }else if(game == Display.GAME_UP){
             if(messagelist.getSelectedIndex()!=0){
                 messagelist.setSelectedIndex(messagelist.getSelectedIndex()-1);
             }else{
                 messagelist.setSelectedIndex(messagelist.size()-1);
             }

        }
    }

    public void shareMessage(int mode) {
         MessagePage messagePage = new MessagePage();
         messagePage.setExitCmd(getExitCmd());
         messagePage.setActionlistener(getActionlistener());
         messagePage.setMode(mode);
         messagePage.setMessage((Message)messagelist.getSelectedItem());
         messagePage.setMessageType(1);
         messagePage.setMessaging(getMessaging());
         messagePage.showPage();
    }

    public void openMessage(int mode) {
         MessagePage messagePage = new MessagePage();
         messagePage.setExitCmd(getExitCmd());
         messagePage.setMessaging(getMessaging());
         messagePage.setActionlistener(getActionlistener());
         messagePage.setIndex(messagelist.getSelectedIndex());        
         messagePage.setMessage((Message)messagelist.getSelectedItem());
         messagePage.setMode(mode);
         messagePage.setMessageType(1);

         messagePage.showPage();

    }

    public void deleteMessage(){
        TextArea body = new TextArea("Delete Message ?", 2, 5);
        body.setGrowByContent(true);
        Command noCommand = new Command("No");
        Command yesCommand = new Command("Yes"){
            public void actionPerformed(ActionEvent evt) {
                getMessageFacade().deleteOutbox(messagelist.getSelectedIndex());
                messageListModel.removeItem(messagelist.getSelectedIndex());
                if(messageListModel.getSize()==0){
                    outboxForm.removeCommand(deleteCmd);
                    outboxForm.removeCommand(deleteAllCmd);
                    outboxForm.removeCommand(detailCmd);
                }
            }
        };
        Dialog.show("", body, new Command[]{noCommand, yesCommand}, Dialog.TYPE_CONFIRMATION, null,0,null);

    }

    public void deleteAllMessage(){

        TextArea body = new TextArea("Delete All Message", 2, 5);
        body.setGrowByContent(true);
        Command noCommand = new Command("No");
        Command yesCommand = new Command("Yes"){
            public void actionPerformed(ActionEvent evt) {
                for(int i=0;i<messageListModel.getSize();i++){
                    getMessageFacade().deleteOutbox(i);
                    messageListModel.removeItem(i);
                    
                }
               
                if(messageListModel.getSize()==0){
                    outboxForm.removeCommand(deleteCmd);
                    outboxForm.removeCommand(deleteAllCmd);
                    outboxForm.removeCommand(detailCmd);
                }
            }
        };
        Dialog.show("", body, new Command[]{noCommand, yesCommand}, Dialog.TYPE_CONFIRMATION, null,0,null);

    }

    public void backToOutboxMenu() {
        OutboxMenu outboxMenu = new OutboxMenu();
        outboxMenu.setExitCmd(getExitCmd());
        outboxMenu.setMessaging(getMessaging());
        outboxMenu.setActionlistener(getActionlistener());
        outboxMenu.showPage();
    }

    public MessageFacade getMessageFacade() {
        if(messageFacade==null){
            messageFacade = new MessageFacade();
        }
        return messageFacade;
    }

    public String getMenuTitle() {
        return MenuString.OUTBOX;
    }
}

